package com.bytedance.retrofit2;

import android.os.Build;
import android.os.Handler;
import android.os.Looper;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.Executor;

public class Platform {
    private static final Platform PLATFORM = Platform.findPlatform();

    Platform() {

    }

    static Platform get() {
        return Platform.PLATFORM;
    }

    static boolean isDefaultMethod() {
        return false;
    }

    private static Platform findPlatform() {
        try {
            Class.forName("android.os.Build");
            if (Build.VERSION.SDK_INT != 0) {
                return new Android();
            }
        } catch (ClassNotFoundException e) {
        }

        try {
            Class.forName("org.robovm.apple.foundation.NSObject");
            return new IOS();
        } catch (ClassNotFoundException e) {
            return new Platform();
        }
    }

    public CallAdapter.Factory defaultCallAdapterFactory(Executor arg2) {
        CallAdapter.Factory v0_1;
        if (arg2 != null) {
            v0_1 = new ExecutorCallAdapterFactory(arg2);
        } else {
            v0_1 = DefaultCallAdapterFactory.INSTANCE;
        }

        return v0_1;
    }

    public Executor defaultCallbackExecutor() {
        return null;
    }

    static final class IOS extends Platform {
        IOS() {

        }

        public final CallAdapter.Factory defaultCallAdapterFactory(Executor arg2) {
            return new ExecutorCallAdapterFactory(arg2);
        }

        public final Executor defaultCallbackExecutor() {
            return new MainThreadExecutor();
        }

        static final class MainThreadExecutor implements Executor {
            private static Object a;
            private static Method b;

            static {
                try {
                    Class v0_1 = Class.forName("org.robovm.apple.foundation.NSOperationQueue");
                    MainThreadExecutor.a = v0_1.getDeclaredMethod("getMainQueue").invoke(null);
                    MainThreadExecutor.b = v0_1.getDeclaredMethod("addOperation", Runnable.class);
                } catch (Exception v0) {
                    throw new AssertionError(v0);
                }
            }

            MainThreadExecutor() {

            }

            public final void execute(Runnable arg5) {
                try {
                    MainThreadExecutor.b.invoke(MainThreadExecutor.a, arg5);
                    return;
                } catch (InvocationTargetException v0) {
                    Throwable v0_3 = v0.getCause();
                    if ((v0_3 instanceof RuntimeException)) {
                        throw (RuntimeException) v0_3;
                    }

                    if ((v0_3 instanceof Error)) {
                        throw (Error) v0_3;
                    }

                    throw new RuntimeException(v0_3);
                } catch (IllegalAccessException v0_1) {
                    throw new AssertionError(v0_1);
                } catch (IllegalArgumentException v0_2) {
                    throw new AssertionError(v0_2);
                }
            }
        }
    }

    static final class Android extends Platform {
        Android() {

        }

        public final CallAdapter.Factory defaultCallAdapterFactory(Executor arg2) {
            return new ExecutorCallAdapterFactory(arg2);
        }

        public final Executor defaultCallbackExecutor() {
            return new MainThreadExecutor();
        }

        final class MainThreadExecutor implements Executor {
            private final Handler a;

            MainThreadExecutor() {
                this.a = new Handler(Looper.getMainLooper());
            }

            public final void execute(Runnable arg2) {
                this.a.post(arg2);
            }
        }
    }
}

